(*  :Title:	Supplemental Inverse z-Transform Rule Base  *)

(*  :Authors:	Brian Evans, James McClellan  *)

(*
    :Summary:	To define those z-transform operations used only
		in multidimensions.  This reduces the amount of
		memory needed to load in the z-transform.
 *)

(*  :Context:	SignalProcessing`Digital`InvZTransform`  *)

(*  :PackageVersion:  2.7	*)

(*
    :Copyright:	Copyright 1989-1991 by Brian L. Evans
		Georgia Tech Research Corporation

	Permission to use, copy, modify, and distribute this software
	and its documentation for any purpose and without fee is
	hereby granted, provided that the above copyright notice
	appear in all copies and that both that copyright notice and
	this permission notice appear in supporting documentation,
	and that the name of the Georgia Tech Research Corporation,
	Georgia Tech, or Georgia Institute of Technology not be used
	in advertising or publicity pertaining to distribution of the
	software without specific, written prior permission.  Georgia
	Tech makes no representations about the suitability of this
	software for any purpose.  It is provided "as is" without
	express or implied warranty.
 *)

(*  :History:	*)

(*  :Keywords:	z-transform, region of convergence	*)

(*
    :Source:	  Dungeon and Mersereau.  {Multidimensional Signal
		Processing}. 1984.
 *)

(*  :Warning:   *)

(*  :Mathematica Version:  1.2 or 2.0  *)

(*  :Limitation:  *)

(*
    :Discussion:  The idea here is to define those z-transform
		  operations which would not be used by undergraduates.
		  They would be loaded by the z-transform rule base
		  when needed.
 *)


(*  :Functions:	  *)



(*  B E G I N     P A C K A G E  *)

BeginPackage[ "SignalProcessing`Digital`MDInvZTransform`" ]
EndPackage[]

BeginPackage[ "SignalProcessing`Digital`InvZTransform`",
	      "SignalProcessing`Digital`ZSupport`",
	      "SignalProcessing`Support`TransSupport`",
	      "SignalProcessing`Support`ROC`",
	      "SignalProcessing`Support`FilterSupport`",
	      "SignalProcessing`Support`SigProc`",
	      "SignalProcessing`Support`LatticeTheory`",
	      "SignalProcessing`Support`SupCode`" ]


If [ TrueQ[ $VersionNumber >= 2.0 ],
     Off[ General::spell ];
     Off[ General::spell1 ] ];


Begin[ "`Private`" ]

(*  Check for non-separable upsampled relationship between z variables  *)

getexponents[ expr_, wlist_ ] :=
	getexponents[ expr, wlist, {} ]
getexponents[ expr_, {}, exponents_ ] :=
	{ expr, exponents }
getexponents[ a_. v_^i_., {v_, vrest___}, exponents_ ] :=
	getexponents[ a, {vrest}, Append[exponents, i] ]
getexponents[ x_, {v_, vrest___}, exponents_ ] :=
	getexponents[ x, {vrest}, Append[exponents, 0] ]

upsampleSystem[ x_, upvarlist_ ] :=
	UpsampledSystem[x, upvarlist, getexponents]

upsampleSystemQ[ x_, upvarlist_ ] :=
	Block [	{upmatrixtrans, zvars},
		{upmatrixtrans, zvars} = upsampleSystem[x, upvarlist];
		TrueQ[ Length[zvars] >= 2 ] &&
		! SameQ[ResamplingMatrix[upmatrixtrans], False] ]


(* ROC adjustment object--- prevents having to make z-transform call itself *)

Format[ newROC[ invztrans_, rest___ ] ] := invztrans

newROC[ f_, {}, {}, {} ] := f
newROC[ myinvz[ f_, z_, n_, rm_, rp_, rest___],
		{z_, zrest___}, rmlist_, rplist_ ] :=
	newROC[ myinvz[f, z, n, First[rmlist], First[rplist], rest],
		{zrest}, Rest[rmlist], Rest[rplist] ]


resampleROC[zvars_, mat_, zlist_, rmlist_, rplist_] :=
	Block [	{dims, j, pos, poslist, rm, rmsublist, rp, rpsublist},

		dims = Length[zvars];

		poslist = Map[ First[First[Position[zlist, #]]]&, zvars ];
		rmsublist = rmlist[[poslist]];
		rpsublist = rplist[[poslist]];
		rmsublist =
			Table[ Apply[ Times, rootROC[ rmsublist, mat[[j]] ] ],
				{j, 1, dims} ];
		rpsublist =
			Table[ Apply[ Times, rootROC[ rpsublist, mat[[j]] ] ],
				{j, 1, dims} ];

		{rm, rp} = {rmlist, rplist};
		For [ j = 1, j <= dims, j++,
		      pos = poslist[[j]];
		      rm[[pos]] = rmsublist[[j]];
		      rp[[pos]] = rpsublist[[j]] ];

		{rm, rp} ]

resampleDialogue[ f_, fz_, mnew_, zvars_ ] :=
	Block [	{nfun, oldindent},
		If [ dialogueAllFlag,
		     Print[ "( after noting that the exponents of " ];
		     Print[ "  ", zvars, " in the expression" ];
		     Print[ "  ", f ];
		     Print[ "  are related by the linear mapping " ];
		     Print[ "  ", mnew, " )" ] ];
		If [ ! SameQ[ResamplingMatrix[mnew], True],
		     Assuming[ ResamplingMatrix[mnew], dialogueAllFlag ] ];
		fz ]

upsampleSetUp[ f_, z_, n_, rm_, rp_, s_, op_, zlist_, rmlist_, rplist_, nlist_ ] :=
        Block [ {invztrans, lnew, lnewtrans, newrm, newrp, nvarsnew, zvarsnew},
		{upmatrixtrans, zvars} = upsampleSystem[f, zlist];
		{lnewtrans, zvarsnew, nvarsnew} =
			ReorderResampling[upmatrixtrans, zvars, zlist, nlist];
		lnew = Transpose[lnewtrans];
		znew = Map[Apply[Times, #]&, Transpose[zlist ^ lnew]];
		fz = f /. ReplaceWith[znew, zlist];
		resampleDialogue[f, fz, lnew, zvarsnew ];
		invztrans = myinvz[fz, z, n, rm, rp, s, op];
		If [ EmptyQ[rmlist] && EmptyQ[rplist],
		     Upsample[lnew, nvarsnew][ invztrans ],
		     {newrm, newrp} =
			resampleROC[zvarsnew, lnew, zlist, rmlist, rplist];
		     dims = Length[zlist];
		     pos = First[First[Position[zlist, z]]];
		     Upsample[lnew, nvarsnew][
			newROC[ invztrans,
				Take[zlist, {pos, dims}],
				Take[newrm, {pos, dims}],
				Take[newrp, {pos, dims}] ] ] ] ]


End[]
EndPackage[]


If [ TrueQ[ $VersionNumber >= 2.0 ],
     On[ General::spell ];
     On[ General::spell1 ] ];


(*  E N D I N G     M E S S A G E  *)

Print["Special multidimensional inverse z-transform rules have been loaded."]
Null
